package com.view.sellmanage;

import com.bean.*;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;
import com.service.PurchaseService;
import com.service.SellService;
import com.service.StockService;
import com.service.impl.PurchaseServiceImpl;
import com.service.impl.SellServiceImpl;
import com.service.impl.StockServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;
import com.view.purchasemanage.GoodsAddDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SellGoodsAddDialog extends JDialog {

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private LovoTable goodsTab;
    private MainFrame frame ;
    private LovoTxt numberTxt;
    private String address;
    private SellService sellService = new SellServiceImpl();
    private StockService stockService = new StockServiceImpl();

    private Sell sell;

    private LovoButton prev,next;

    private JLabel txtPage;
    private PageUtilSuper<Stock> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;
    private User user;
    private String newAddress;

    private Customer customer;

    public SellGoodsAddDialog(MainFrame frame, String address, Customer customer) {
        // TODO Auto-generated constructor stub
        super(frame,"添加商品",true);
        this.frame = frame;
        this.newAddress = address;
        this.user = frame.getUser();
        this.customer = customer;
        this.sell = getSell();

        this.setBounds(350, 250, 700, 600);
        this.setLayout(null);
        setPageUtilSuper();
        this.tableInit();
        this.init();
        this.setVisible(true);

        txtPage = new JLabel();
        this.txtPage.setBounds(170, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private Sell getSell(){
        Sell newSell = new Sell();
        newSell = sellService.sellInit(user,customer,newAddress);
        return newSell;
    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        java.util.List<Stock> stocks = new ArrayList<>();
        stocks = stockService.showStocks();
        pageUtilSuper = new PageUtilSuper(pageSize,stocks);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        java.util.List<Stock> stocks = new ArrayList<>();
        stocks = pageUtilSuper.getPgNumberThing(pgNumber);
        goodsTab.updateLovoTable(stocks);
    }

    private void tableInit(){
        this.goodsTab = new LovoTable(this, new String[]{"商品名称",
                "厂家", "销售价","库存"}, new String[]{"goods.name","goods.manufactor","goods.sellPrice","number"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        java.util.List<Stock> stocks = new ArrayList<>();
        stocks = pageUtilSuper.getPgNumberThing(1);
        goodsTab.updateLovoTable(stocks);
        this.goodsTab.setSizeAndLocation(0, 0, 250, 200);
    }

    private void init() {
        // TODO Auto-generated method stub


        this.numberTxt = new LovoTxt("数量", 120, 400, this);
        this.addBtn = new LovoButton("添加", 120, 520, this);
        this.cancelBtn = new LovoButton("取消", 270, 520, this);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = goodsTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null, "请选择一行");
                }else {
                        int numberTxtText = Integer.parseInt(numberTxt.getText());
                        int m = sellService.addGoods(key,numberTxtText,sell);
                        if (m > 0) {
                            JOptionPane.showMessageDialog(null, "添加成功");
                            frame.flush();
                            SellGoodsAddDialog.this.dispose();
                        } else {
                            JOptionPane.showMessageDialog(null, "添加失败");
                        }
                }
            }
        });

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SellGoodsAddDialog.this.dispose();
            }
        });

    }

}
